package com.net;

import java.io.*;

/**
 * @Author : hebad90@163.com
 * @Date : 2014/10/23 19:12
 * @From : javalearn
 */
public class URLTest {
    public static void main(String[] args) throws Exception {
        File root = new File("E:\\ideaWorksapce\\work\\git_workspace\\zhanggui-crm-all\\zhanggui-crm-logistics-notify\\src\\main\\java\\com\\raycloud\\crm\\logistics\\marketing\\service\\");
        disposeFile(root);
    }

    public static void disposeFile(File root){
        File[] files = root.listFiles();
        if(files!=null){
            for(File f:files){
                if(f.isDirectory() && !f.getName().startsWith(".") && f.getName().startsWith("zhanggui-crm")){
                    disposeFile(f);
                }else{
                    analyzeFile(f);
                }
            }
        }
    }

    public static void analyzeFile(File file){
        try {
            File newFile = new File(file.getParent(),file.getName()+"_1");
            if(!newFile.exists()){
                newFile.createNewFile();
            }
            System.out.println(file.getAbsolutePath());
            if(!file.canRead()){
                System.err.println(file+" 不能读");
                return;
            }
            BufferedReader reader =null;
            PrintWriter writer =null;
            try{
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
                writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(newFile),"utf-8"));
            }catch (Exception e){
                return;
            }


            String line ="";
            while((line=reader.readLine())!=null){
                String newLine=removeImpl(line);
                writer.println(newLine);
            }
            reader.close();
            writer.close();
            if(file.delete()){
                newFile.renameTo(new File(file.getParent(),file.getName()));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String removeImpl(String line){
        if(line.contains("import com.raycloud.crm.util.DBRouter;")){
            return "";
        }
        if(line.contains("@Autowired")){
            return "";
        }
        if(line.contains("private ")){
            String newLine= "@Autowired\n"+line;
            return newLine;
        }else{
            return line;
        }

    }
}
